package com.example.test003.controller;


import com.example.test003.domain.ResponseResult;
import com.example.test003.enums.AppHttpCodeEnum;
import com.example.test003.pojo.Problem;
import com.example.test003.service.ProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class ProblemController {

    @Autowired
    private ProblemService problemService;

    @GetMapping("/getproblems")
    public List<Problem> getProblems(){
        List<Problem> problems = problemService.getProblems();
        return problems;
    }

    @PostMapping("/submit")
    public ResponseResult submit(String suggestion){
        if (suggestion.isEmpty()){
            return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }
        if (suggestion==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }
        Date currentDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = dateFormat.format(currentDate);
        System.out.println(formattedDate);
        int i = problemService.submitProblem(suggestion, 1, formattedDate);
        if (i==1){
            return ResponseResult.okResult();
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }
    }
}
